package cn.lutu.alipay.service.impl;

import cn.lutu.alipay.pojo.dto.AlipayOrderDTO;
import cn.lutu.alipay.service.AlipayOrderService;
import cn.lutu.alipay.mapper.AlipayOrderMapper;
import cn.lutu.alipay.pojo.entity.AlipayOrder;
import cn.lutu.util.StringUtils;
import cn.lutu.util.SysConstant;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class AlipayOrderServiceImpl extends ServiceImpl<AlipayOrderMapper, AlipayOrder> implements AlipayOrderService {
@Autowired
private AlipayOrderMapper alipayOrderMapper;
    /**
     * 创建订单
     * @return
     */
    @Override
    public AlipayOrder createOrder(AlipayOrderDTO alipayOrderDTO) {
       //1.生成订单编号
        System.out.println("alipayOrderDTO = " + alipayOrderDTO);
        AlipayOrder alipayOrder =new AlipayOrder();
        BeanUtils.copyProperties(alipayOrderDTO,alipayOrder);
        log.debug("alipayInfo"+alipayOrder);
        alipayOrder.setOrderNo(StringUtils.createOrderNum());
        alipayOrder.setPayType("支付宝");
        alipayOrder.setStatus(SysConstant.WAIT_BUYER_PAY);
        alipayOrder.setUseStatus(0);
        if(alipayOrderDTO.getOrderNo()==null){
            alipayOrderMapper.insert(alipayOrder);
        }else{
            alipayOrderMapper.updateById(alipayOrder);
        }

        System.out.println(alipayOrder);
        return alipayOrder;
    }
//根据订单编号获取订单
    @Override
    public AlipayOrder getByTradeNo(String outTradeNo) {
        //定义查询条件
        QueryWrapper<AlipayOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_no",outTradeNo);
        AlipayOrder alipayOrder =  alipayOrderMapper.selectOne(queryWrapper);
        return alipayOrder;
    }
//更新订单信息
    @Override
    public void updateOrderStatus(AlipayOrder alipayOrder, String tradeSuccess) {
         alipayOrder.setStatus(tradeSuccess);
         System.out.println(tradeSuccess);
         alipayOrderMapper.updateById(alipayOrder);
    }
}
